package com.lanlengran.pulltorefreshtest.myview;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanlengran.pulltorefreshtest.R;

import butterknife.BindView;

/**
 * Created by ruiqin on 16-9-1.
 */
public class RefreshableView extends LinearLayout implements View.OnTouchListener {
    @BindView(R.id.arrow)
    ImageView arrow;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.description)
    TextView description;
    @BindView(R.id.updated_at)
    TextView updatedAt;
    @BindView(R.id.pull_to_refresh_head)
    RelativeLayout pullToRefreshHead;
    private View header;
    private MarginLayoutParams headerLayoutParams;
    private int hideHeaderHeight=0;
    private double ydown,ymove;
    private View sonView;
    private boolean isChange=true;
    private int lastHeight;
    //回滚速度
    private int speed=10;
    private boolean isFlash=false;

    public RefreshableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        header = LayoutInflater.from(context).inflate(R.layout.pull_to_refresh, null);
        description= (TextView) header.findViewById(R.id.description);
        setOrientation(VERTICAL);
        addView(header,0);

    }

    public View getSonView() {
        return sonView;
    }

    public void setSonView(View sonView) {
        this.sonView = sonView;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);


        if (isChange){
            hideHeaderHeight = -header.getHeight();
            headerLayoutParams = (MarginLayoutParams) header.getLayoutParams();
            headerLayoutParams.topMargin = hideHeaderHeight;

            sonView.setOnTouchListener(this);
            isChange=true;
        }

    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
      //  Log.i("qin",ydown+"");
        if (motionEvent.getAction()==MotionEvent.ACTION_DOWN){
            ydown=motionEvent.getRawY();
       //     Log.i("qin",ydown+"");
        }
        if (motionEvent.getAction()==MotionEvent.ACTION_MOVE){

            ymove=motionEvent.getRawY();
            int downHeight=  (hideHeaderHeight)+(int)(ymove-ydown)/2;
            //屏蔽上划
            if (downHeight<hideHeaderHeight&&headerLayoutParams.topMargin<=hideHeaderHeight){
                return false;
            }
            if (downHeight>(-hideHeaderHeight*1.5)){
                downHeight= (int) (-hideHeaderHeight*1.5);
                isFlash=true;
                description.setText(getResources().getString(R.string.release_to_refresh));
            }
            headerLayoutParams.topMargin= downHeight;
            header.setLayoutParams(headerLayoutParams);

            lastHeight=downHeight;

//            Log.i("qin","topMargin="+downHeight);
//            Log.i("qin","hideHeaderHeight="+hideHeaderHeight);
            Log.i("qin","headerLayoutParams.topMargin="+headerLayoutParams.topMargin);
        }
        if (motionEvent.getAction()==MotionEvent.ACTION_UP){
            Log.i("qin","ACTION_UP+headerLayoutParams.topMargin="+headerLayoutParams.topMargin);
           new HideHeaderTask().execute();
        }

        return false;
    }

    private boolean setIsAbleToPull(MotionEvent event) {
        if (sonView instanceof ListView){
            sonView=(ListView)sonView;
        }
        if (sonView instanceof RecyclerView){
            sonView=(ListView)sonView;
        }
        return false;
    }

    class HideHeaderTask extends AsyncTask<Void, Integer, Integer>{


        @Override
        protected Integer doInBackground(Void... params) {
            int topMargin=lastHeight;
            while (true){
                topMargin=topMargin-speed;
                if (topMargin<=hideHeaderHeight){
                    break;
                }
                Log.i("HideHeaderTask","topmargin="+topMargin);
                publishProgress(topMargin);
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return topMargin;
        }


        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            description.setText(getResources().getString(R.string.pull_to_refresh));
            headerLayoutParams.topMargin=values[0];
            header.setLayoutParams(headerLayoutParams);
            Log.i("HideHeaderTask","values="+values[0]);
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            headerLayoutParams.topMargin=integer;
            header.setLayoutParams(headerLayoutParams);
        }
    }
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        //  Log.i("qin",ydown+"");
//        if (event.getAction()==MotionEvent.ACTION_DOWN){
//            ydown=event.getRawY();
//            //     Log.i("qin",ydown+"");
//        }
//        if (event.getAction()==MotionEvent.ACTION_MOVE){
//            ymove=event.getRawY();
//           int downHeight=  (hideHeaderHeight)+(int)(ymove-ydown)/2;
//            headerLayoutParams.topMargin= downHeight;
//            header.setLayoutParams(headerLayoutParams);
//
//            Log.i("qin",ymove-ydown+"topMargin");
//            Log.i("qin","hideHeaderHeight="+downHeight);
//        }
//        return super.onTouchEvent(event);
//    }
}
